package com.yc.demo.d0224;

import com.google.gson.Gson;
import com.yc.demo.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "NameServlet", value = "/NameServlet")
public class NameServlet extends HttpServlet {
    public static String[] names = {"Abe","Abraham","Acheson","Ackerman(n)","Adam","Adams","Bnana","Bill","Apple"};
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        final String name = request.getParameter("name");
        for (String n : names){
            if(n.equals(name)){
                Gson gson = new Gson();
                Result result = new Result(0,"该用户已被注册",new int[]{1,2,3,4,5,6});
                String json = gson.toJson(result);
                response.getWriter().printf(json);
                return;
            }
        }
        Gson gson = new Gson();
        Result result = new Result(1,"该用户未被注册",new int[]{1,2});
        String json = gson.toJson(result);
        response.getWriter().printf(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
